;(function () {
	new Vue({
		el: '#expenditure-config',
		data: {
			projectId: hlUrlUtil.GetQueryString('projectId'),
			templateLt: [],
			expendLt: {
				expenditureBootListO:[],
				expenditureBootListP:[],
				expenditureBootListS:[]
			},
			freeIds: [],
			freeNames: [],
			moduleflga: false,
			freeflag: false,
			templateFlag: true,
			moduleContain: [],
			createflag: true,
			projectJs: {},
			templateName: '',
			templateId: '',
			billTemplateLt: [],
			addBillTemplateLt: [],
			billAllTemplateDt: [],
			auth: JSON.parse(localStorage.getItem('permissions'))
		},
		methods: {
			templateIdDt () {
				hlhttp.get({
					url: 'expenditure_templates',
					success: (data) => {
						this.templateLt = data;
					}
				})
			},
			validExpenditures () {
				hlhttp.get({
					url: 'project/valid_expenditures',
					success: (data) => {
						this.expendLt = data;
					}
				})
			},
			//自由配置
			freeConfig (data) {
				if (!this.freeflag) {
					hl.toast('请选择自由配置')
					return
				}
				if (this.freeIds.indexOf(data.id) > -1) {
					this.freeIds.splice(this.freeIds.indexOf(data.id), 1)
					if (this.freeNames.indexOf(data.name) > -1) {
						this.freeNames.splice(this.freeNames.indexOf(data.name), 1)
						return
					}
					return
				}

				this.freeIds.push(data.id)
				this.freeNames.push(data.name)
			},
			//模板选择
			moduleSelect () {
				this.freeflag = false;
				this.moduleflga = !this.moduleflga
			},
			//自由配置
			freeSelect () {
				this.moduleflga = false;
				this.templateName = '';
				this.templateId = '';
				this.freeflag = !this.freeflag;
				if (!this.freeflag) {
					this.freeIds = []
				}
			},
			//模板选择
			templateSel (data) {
				if (!this.moduleflga) {
					this.moduleflga = true
				}
				this.templateName = data.name;
				this.templateId = data.id;
				this.moduleContain = data.expenditures
			},
			//生成新模板
			createModule () {
				this.createflag = !this.createflag
			},
			//获取项目信息
			projectDt () {
				hlhttp.get({
					url: 'project',
					data: {project_id: this.projectId},
					success: (data) => {
						this.projectJs = data;
						if (data.templateName) {
							this.templateName = data.templateName;
							this.templateId = data.templateId;
							this.$refs.modules.innerHTML = data.templateName;
							this.moduleContain = data.expenditures;
							this.moduleflga = true;
						}
					}
				})
			},
			//保存编辑模板
			saveExpend () {
				if (!this.moduleflga && !this.freeflag) {
					hl.toast('请选择模板')
					return
				}
				let freeIds = [];
				if (this.freeflag) {
					freeIds = JSON.parse(JSON.stringify(this.freeIds))

					if (!this.templateName) {
						hl.toast('新模板名称不能为空')
						return
					}
				} else {
					this.moduleContain.map(item => {
						freeIds.push(item.expenditureId)
					})
				}
				if (!freeIds.length) {
					hl.toast('请选择费项')
					return
				}
				hlhttp.put({
					url: 'projects/expenditure',
					data: {
						project_id: this.projectId,
						template_id: this.templateId || '#',
						expenditure_ids: freeIds.toString() || '#',
						template_name: this.templateName,
						rent_late_fee_percent: this.projectJs.rentLateFeePercent || '#',
						contract_no_prefix: this.projectJs.contractNoPrefix || '#',
						contract_name_prefix: this.projectJs.contractNamePrefix || '#',
						expenditure_template_flag: this.freeflag && this.templateFlag ? 'Y' : 'N'
					},
					success: () => {
						hl.toast('保存成功')
						if (this.freeflag) {
							this.freeIds = [];
							this.freeNames = [];
							this.freeflag = false;
						}
						this.projectDt()
						this.templateIdDt()
					}
				})
			},
			//获取已经配置账单模板
			billTemplateDt () {
				hlhttp.get({
					url: 'project/print_template_list',
					data: {project_id: this.projectId},
					success: (data) => {
						if (data[0].type == 'S') {
							return
						}
						this.billTemplateLt = data;
					}
				})
			},
			//获取全部账单模板
			billAllTemplate () {
				hlhttp.get({
					url: 'print_template_list',
					success: (data) => {
						this.billAllTemplateDt = data;
					}
				})
			},
			//添加账单模板
			addTemplateDt () {
				if (this.addBillTemplateLt.length) {
					hl.toast('请选择账单模板')
					return
				}
				this.addBillTemplateLt.push({
					name: '',
					id: '',
					isDefault: 'N'
				})
			},
			//选择账单模板
			billAllTemplateSel (data, i) {
				this.addBillTemplateLt[i].name = data.name;
				this.addBillTemplateLt[i].id = data.id;
				setTimeout(() => {
					this.billTemplateLt.push(this.addBillTemplateLt[i])
					this.addBillTemplateLt = [];
				}, 30)
			},
			//设为默认
			defaultSel (i) {
				if (this.addBillTemplateLt.length) {
					hl.toast('请选择账单模板')
					return
				}
				this.billTemplateLt.map(item => {
					item.isDefault = 'N'
				})
				this.billTemplateLt[i].isDefault = this.billTemplateLt[i].isDefault == 'Y' ? 'N' : 'Y';
			},
			//保存账单模板
			saveBillTemplate () {
				let billTemplateLt = [];
				this.billTemplateLt.map(item => {
					billTemplateLt.push({template_id: item.id, template_name: item.name, is_default: item.isDefault})
				})
				hlhttp.post({
					url: 'project/print_template/_bind',
					data: {
						project_id: this.projectId,
						project_name: this.projectJs.name,
						project_templates: JSON.stringify(billTemplateLt)
					},
					success: () => {
						hl.toast('保存账单模板成功')
					}
				})
			},
			//账单删除---新增
			delAddBill (i) {
				this.addBillTemplateLt.splice(i, 1)
			},
			//账单删除---编辑
			delEditBill (i) {
				this.billTemplateLt.splice(i, 1)
			}
		},
		created () {
			hlSelectUi()
		},
		mounted () {
			this.billAllTemplate()
			this.templateIdDt()
			this.validExpenditures()
			this.projectDt()
			this.billTemplateDt()
		}
	})
} ())
